#!/bin/bash

# 测量网口每秒数据包

INTERVAL="1"  # update interval in seconds

if [ -z "$1" ]; then
        echo
        echo usage: $0 [network-interface]
        echo
        echo e.g. $0 eth0
        echo
        echo shows packets-per-second
        exit
fi

IF=$1

while true
do
        R1=`cat /sys/class/net/$1/statistics/rx_packets`
        T1=`cat /sys/class/net/$1/statistics/tx_packets`
        sleep $INTERVAL
        R2=`cat /sys/class/net/$1/statistics/rx_packets`
        T2=`cat /sys/class/net/$1/statistics/tx_packets`
        TXPPS=`expr $T2 - $T1`
        RXPPS=`expr $R2 - $R1`
        echo "TX $1: $TXPPS pkts/s RX $1: $RXPPS pkts/s"
done


# /sys/class/net/eth0/statistics/rx_packets: 收到的数据包数据
# /sys/class/net/eth0/statistics/tx_packets: 传输的数据包数量
# /sys/class/net/eth0/statistics/rx_bytes: 接收的字节数
# /sys/class/net/eth0/statistics/tx_bytes: 传输的字节数
# /sys/class/net/eth0/statistics/rx_dropped: 当收到包数据包下降的数据量
# /sys/class/net/eth0/statistics/tx_dropped: 传输包数据包下降的数据量